<?php

/**
 * 代金卡申领单 ajax类
 * @package  ajax
 * @subpackage  backend
 * @create by allen.qiang
 */
header('Content-Type:text/html;charset=utf-8');
//require 'module/base/cardHandler.class.php';

class ajax_cardApply {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //代金卡申领单列表
                    $this->cardApplyList();
                    break;

                case 2: //新增或编辑代金卡申领单
                    $this->cardApplyAdd();
                    break;

                case 3: //删除代金卡申领单
                    $this->cardApplyDel();
                    break;

                case 4: //发卡
                    $this->fkCardApply();
                    break;

                case 5: //添加一条代金卡详情,对卡号段的判断
                    $this->addOneApply();
                    break;

                case 6: //回款列表
                    $this->sectionList();
                    break;

                case 7: //添加或编辑回款信息
                    $this->editCardHK();
                    break;

                case 8: //审核回款单
                    $this->auditCardHK();
                    break;

                case 9: //发票列表
                    $this->searchCardInvoice();
                    break;

                case 10: //保存或修改开票信息
                    $this->editCardInvoice();
                    break;

                case 11: //审核发票
                    $this->auditCardInvoice();
                    break;

                case 12: //发票冲红
                    $this->chCardInvoice();
                    break;

                case 13: //申领单页面选择代金卡时查询代金卡信息
                    $this->searchCardInfo();
                    break;
                case 14: //短信提醒修改申领单
                    $this->goTosms();
                    break;
                case 15://删除发票申请
                    $this->delApply_invoice();
                    break;


            }

        }

    }

    //获取代金卡申领单列表
    public function cardApplyList(){

        global $G_PGLOB;

        //查询条件
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_data	  = $cardHandler->searchCardApply( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result_data) {
            foreach($result_data as &$cardApplyInfo) {

                //代金卡申领单状态字符串
                $cardApplyStatus = '';

                //回款状态
                $sectionStatus = '';

                if( $cardApplyInfo['status'] == 1 ){

                    $cardApplyStatus .= '申请中';

                    $sectionStatus .= '未回款';

                }elseif ( $cardApplyInfo['status'] == 2 ){

                    $cardApplyStatus .= '已发卡';

                    $sectionStatus .= '未回款';

                }elseif ( $cardApplyInfo['status'] == 3 ){

                    $sectionStatus .= '已回款';

                }

                //操作字符串
                $operatorString = '<a href="javascript: void(0);" onclick="cardApplyList.detail(' . $cardApplyInfo['applyID'] . ')">详情</a>&nbsp;';

                if( $cardApplyInfo['status'] == 1 ){
                    $operatorString .= '<a href="javascript: void(0);" onclick="cardApplyList.edit(' . $cardApplyInfo['applyID'] . ')">编辑</a>&nbsp;'.
                        '<a href="javascript: void(0);" onclick="cardApplyList.sendCard(' . $cardApplyInfo['applyID'] . ')">发卡</a>';
                }

                if( $cardApplyInfo['status'] == 2 || $cardApplyInfo['status'] == 3){

                    $operatorString .= '<a href="javascript: void(0);" onclick="cardApplyList.section(' . $cardApplyInfo['applyID'] . ')">回款</a>&nbsp;'.
                                       '<a href="javascript: void(0);" onclick="cardApplyList.invoice(' . $cardApplyInfo['applyID'] . ')">开票</a>';
                }

                $result_array['rows'][] = array(
                    'id' 	=> $cardApplyInfo['applyID'],
                    'cell' 	=> array(
                        $operatorString ,
                        $cardApplyInfo['applyID'] ,
                        $cardApplyInfo['custname'],
                        $cardApplyInfo['connectway'],
                        $cardApplyInfo['applyPerson'],
                        $cardApplyInfo['applyTime'],
                        $cardApplyInfo['createTime'],
                        $cardApplyInfo['totalNumber'],
                        $cardApplyInfo['totalAmount'],
                        $cardApplyStatus,
                        $sectionStatus
                    )
                );
            }
        }

        echo json_encode( $result_array );
    }

    /**
     * 更新或保存代金卡申领单信息
     */
    private function cardApplyAdd() {

        global $G_PGLOB;

        /**
         * 代金卡申领单状态：1.申请中 2，已发卡 3.已回款
         */

        if( empty($_REQUEST['cardInfo']) ){

            echo json_encode( array('code'=>400 , 'message'=>'请添加申领的代金卡') );
            return;

        }

        foreach ( $_REQUEST['cardInfo'] as $cardArr ){

            //分解面值数值
            $arr = explode('-',$cardArr['faceValue']);

            $faceValue = $arr[0];

            $productID = $arr[1];

            $cardInfo[] = array(
                'faceValue'=>$faceValue,
                'productID'=>$productID,
                'startID'=>$cardArr['startID'],
                'endID'=>$cardArr['endID'],
                'cardNumber'=>$cardArr['cardNumber'],
                'discount'=>$cardArr['discount'],
                'totalAmount'=>$cardArr['totalAmount']
            );

        }

        $_REQUEST['cardInfo'] = $cardInfo;

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //调用卡管理类中的更新代金卡申领单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->editCardApply( $_REQUEST ) ;

        echo json_encode($result_array);

//        echo json_encode( array('code'=>200 , 'message'=>'ok') );

    }

    /**
     * 删除代金卡申领单信息
     *
     */
    private function cardApplyDel () {

        global $G_PGLOB;

        //接收参数
        $delArr = $_REQUEST['delArr'];

        //调用卡管理类中的删除代金卡申领单方法
        $cardHandler  = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->delCardApply( $delArr ) ;

        echo json_encode($result_array);

    }

    //发卡
    public function fkCardApply(){

        global $G_PGLOB;

        //调用卡管理类中的审核代金卡申领单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array 		 = $cardHandler->fkCardApply( $_REQUEST['applyID'] ) ;

        echo json_encode($result_array);

    }

    //添加一条代金卡申领，对卡号段判断
    function addOneApply(){

        global $G_PGLOB;

        //分解面值字符串
        $arr = explode('-',$_REQUEST['faceValue']);

        $_REQUEST['faceValue'] = $arr[0];

        $_REQUEST['productID'] = $arr[1];

        //调用卡管理类中的检查卡号段方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array 		 = $cardHandler->checkCard( $_REQUEST ) ;

        echo json_encode( $result_array );
        return;

    }

    //获取申领单回款列表
    public function sectionList(){

        global $G_PGLOB;

        //查询条件
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_data	  = $cardHandler->searchCardHK( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result_data) {
            foreach($result_data as &$sectionInfo) {

                if( $sectionInfo['status'] == 0 ){

                    $sectionStatus = '未回款';

                }elseif ( $sectionInfo['status'] == 1 ){

                    $sectionStatus = '已回款';

                }

                if( $sectionInfo['hkType'] == 1 ){
                    $sectionType = '现金';
                }elseif ( $sectionInfo['hkType'] == 2 ){
                    $sectionType = 'pos';
                }elseif ( $sectionInfo['hkType'] == 3 ){
                    $sectionType = '支票';
                }elseif ( $sectionInfo['hkType'] == 4 ){
                    $sectionType = '汇款';
                }

                //操作字符串
                $operatorString = '';

                if( $sectionInfo['status'] == 0 ){
                    $operatorString .= '<a href="javascript: void(0);" onclick="section.edit(' . $sectionInfo['hkID'] . ',' . $sectionInfo['applyID'] . ',\'' . $sectionInfo['timeStamp'] . '\',' . $sectionInfo['hkType'] . ',' . $sectionInfo['hkAmount'] .',\'' . $sectionInfo['hkTime'] .'\',\'' . $sectionInfo['hkPerson'] . '\')">编辑</a>&nbsp;&nbsp;'.
                        '<a href="javascript: void(0);" onclick="section.sectionConfirm(' . $sectionInfo['hkID'] . ',' .$sectionInfo['applyID'] . ')">回款</a>';
                }

                $result_array['rows'][] = array(
                    'id' 	=> $sectionInfo['hkID'],
                    'cell' 	=> array(
                        $operatorString ,
                        $sectionInfo['hkID'] ,
                        $sectionType,
                        $sectionInfo['hkAmount'],
                        $sectionInfo['hkPerson'],
                        $sectionInfo['hkTime'],
                        $sectionStatus
                    )
                );
            }
        }

        echo json_encode( $result_array );
    }

    /**
     * 更新或保存回款单
     */
    private function editCardHK() {

        global $G_PGLOB;

        /**
         * 代金卡申领单状态：1.申请中 2，已发卡 3.已回款
         */

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        if( isset($_REQUEST['hkID']) && !empty($_REQUEST['hkID']) ){

            //查询回款单信息
            $sectionObj = new entity('card_apply_huikuan');
            $sectionObj->hkID = $_REQUEST['hkID'];

            if( !$G_PGLOB->DBCA->fetch($sectionObj) ){
                echo json_encode( array( 'code'=>404 , 'message'=>'没有找到信息，请刷新页面' ) );
                return;
            }

            if( $sectionObj->status == 1 ){
                echo json_encode( array( 'code'=>404 , 'message'=>'该回款单已经审核' ) );
                return;
            }

            if( $sectionObj->timeStamp != $_REQUEST['timeStamp'] ){
                echo json_encode( array( 'code'=>404 , 'message'=>'当前页面已过期，请刷新页面' ) );
                return;
            }

        }

        //调用卡管理类中的更新代金卡申领单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->editCardHK( $_REQUEST ) ;

        echo json_encode($result_array);

    }

    /**
     * 审核回款单
     */
    function auditCardHK(){

        global $G_PGLOB;

        /**
         * 代金卡申领单状态：1.申请中 2，已发卡 3.已回款
         */

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //调用卡管理类中的更新代金卡申领单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->auditCardHK( $_REQUEST['hkID'] , $_REQUEST['applyID'] ) ;

        echo json_encode($result_array);

    }

    //获取申领单开票列表
    public function searchCardInvoice(){

        global $G_PGLOB;

        //查询条件
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_data	  = $cardHandler->searchCardInvoice( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result_data) {
            foreach($result_data as &$invoiceInfo) {

                if( $invoiceInfo['status'] == 0 &&  $invoiceInfo['rcwflg'] == 0){

                    $invoiceStatus = '未开票';

                }elseif ( $invoiceInfo['status'] == 1 && $invoiceInfo['rcwflg'] == 0 ){

                    $invoiceStatus = '已开票';

                }elseif ( $invoiceInfo['status'] == 1 && $invoiceInfo['rcwflg'] == 1 ) {
                    $invoiceStatus = '<font color="red">已冲红</font>';
                }else{
                    $invoiceStatus = '';
                }

                //操作字符串
                $operatorString = '';

                if( $invoiceInfo['status'] == 0 && $invoiceInfo['rcwflg'] == 0 ){
                    $operatorString .= '<a href="javascript: void(0);" onclick="invoice.edit(' . $invoiceInfo['invoiceID'] . ',' . $invoiceInfo['applyID'] . ',\'' . $invoiceInfo['timeStamp'] . '\',\'' . $invoiceInfo['invoiceTitle'] . '\',' . $invoiceInfo['invoiceNo'] .',\'' . $invoiceInfo['invoiceContent'] .'\',\'' . $invoiceInfo['invoiceTime'] .  '\',' . $invoiceInfo['invoiceAvg'] . ',' . $invoiceInfo['invoiceNum'] . ',' . $invoiceInfo['invoiceAmount'] . ')">编辑</a>&nbsp;&nbsp;'.
                        '<a href="javascript: void(0);" onclick="invoice.invoiceConfirm(' . $invoiceInfo['invoiceID'] . ',' .$invoiceInfo['applyID'] . ')">开票</a>&nbsp;&nbsp;';
                }

                if( $invoiceInfo['status'] == 1 && $invoiceInfo['rcwflg'] == 0 ){
                    $operatorString .= '<a href="javascript: void(0);" onclick="invoice.rcwConfirm(' . $invoiceInfo['invoiceID'] .')">冲红</a>';
                }

                $result_array['rows'][] = array(
                    'id' 	=> $invoiceInfo['invoiceID'],
                    'cell' 	=> array(
                        $operatorString ,
                        $invoiceInfo['invoiceID'] ,
                        $invoiceInfo['invoiceTitle'],
                        $invoiceInfo['invoiceNo'] ,
                        $invoiceInfo['invoiceTime'],
                        $invoiceInfo['invoiceContent'],
                        $invoiceInfo['invoiceAvg'],
                        $invoiceInfo['invoiceNum'],
                        $invoiceInfo['invoiceAmount'],
                        $invoiceStatus
                    )
                );
            }
        }

        echo json_encode( $result_array );
    }

    /**
     * 更新或保存开票信息
     */
    private function editCardInvoice() {

        global $G_PGLOB;

        /**
         * 代金卡申领单状态：1.申请中 2，已发卡 3.已回款
         */

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        if( isset($_REQUEST['invoiceID']) && !empty($_REQUEST['invoiceID']) ){

            //查询回款单信息
            $invoiceObj = new entity('card_apply_invoice');
            $invoiceObj->invoiceID = $_REQUEST['invoiceID'];

            if( !$G_PGLOB->DBCA->fetch($invoiceObj) ){
                echo json_encode( array( 'code'=>404 , 'message'=>'没有找到发票信息，请刷新页面' ) );
                return;
            }

            if( $invoiceObj->status == 1 ){
                echo json_encode( array( 'code'=>404 , 'message'=>'该发票已经开票' ) );
                return;
            }

            if( $invoiceObj->timeStamp != $_REQUEST['timeStamp'] ){
                echo json_encode( array( 'code'=>404 , 'message'=>'当前页面已过期，请刷新页面' ) );
                return;
            }

        }

        //调用卡管理类中的更新代金卡申领单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->editCardInvoice( $_REQUEST ) ;

        echo json_encode($result_array);

    }

    /**
     * 开票
     */
    function auditCardInvoice(){

        global $G_PGLOB;

        //调用卡管理类中的发票审核方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->auditCardInvoice( $_REQUEST['invoiceID'] , $_REQUEST['applyID'] ) ;

        echo json_encode($result_array);

    }

    /**
     * 发票冲红
     */
    function chCardInvoice(){

        global $G_PGLOB;

        //调用卡管理类中的更新代金卡申领单方法
        $cardHandler = new cardHandler( $G_PGLOB->DBCA ) ;
        $result_array = $cardHandler->chCardInvoice( $_REQUEST['invoiceID'] ) ;

        echo json_encode($result_array);

    }

    /**
     * 申领单页面选择代金卡时查询代金卡信息
     */
    function searchCardInfo( ){

        global $G_PGLOB;

        //实例化卡管理类
        $cardHandler = new cardHandler( $G_PGLOB->DBCA );

        //查询生产单信息
        $productInfo = $cardHandler->searchCardProduct( $_REQUEST );

        if( empty($productInfo) ){
            echo json_encode( array( 'code'=>404 , 'message'=>'没有找到该代金卡所在生产单信息' ) );
            return;
        }

        //查询该生产单代金卡的始末编号
        $searchCardInfo_sql = 'SELECT MIN(cardID) startID ,MAX(cardID) endID FROM card_detail WHERE productID = ?';
        $cardInfo           = $G_PGLOB->DBCA->stmt_query( $searchCardInfo_sql , 'i' , $_REQUEST['productID'] );

        if( empty($cardInfo) ){
            echo json_encode( array( 'code'=>404 , 'message'=>'没有找到该代金卡信息' ) );
            return;
        }

        //重新卡组装数据
        $cardArr['startID'] = $cardInfo[0]->startID;
        $cardArr['endID']   = $cardInfo[0]->endID;

        echo json_encode( array( 'code'=>200 , 'productInfo'=>$productInfo[0] , 'cardInfo'=>$cardArr ) );
        return;

    }

    /*
     * 短息提醒修改申领单
     */
    function goTosms(){
        error_log(print_r("-------",1));
        //加载类文件
        quanli_include('module/base/common/quanliSms.class.php');
        global $G_PGLOB;
        //实例化短信类
        $quanliSms = new quanliSms();
        //发送短信
        $quanliSms->sendSMS($_REQUEST['mobile'], $_REQUEST['comment'] );

        die(json_encode(array('code' => 200, 'message' => '提醒成功')));




    }
    /*
     * 删除发票申请
     */
    function delApply_invoice(){

        global $G_PGLOB;
        //申请单ID
        $invoice_id= $_REQUEST['invoiceID'];

        if(empty($invoice_id)){
            die(json_encode(array('code' => 404, 'message' => '该发票申请单不存在')));
        }
        try{

            //查询出此条发票申请记录申领单ID 和 金额
            $selSQL='select apply_id,invoice_amount from card_apply_invoice where invoice_id=? ';
            $cardArray=$G_PGLOB->DBCA->stmt_query($selSQL,'i',$invoice_id);


            //查询该申领单开票金额
            $selSQL_1='select  invoice_amount from card_apply where apply_id=? ';
            $cardArray_1=$G_PGLOB->DBCA->stmt_query($selSQL_1,'i',$cardArray[0]->apply_id);

            //还原申领单开票金额
            $cardApply=new entity('card_apply');
            $cardApply->apply_id=$cardArray[0]->apply_id;
            $G_PGLOB->DBCA->fetch($cardApply);
            $cardApply->invoice_amount=$cardArray_1[0]->invoice_amount-$cardArray[0]->invoice_amount;
            $G_PGLOB->DBCA->save($cardApply);

            //删除此条发票申请记录
            $delSql='delete from card_apply_invoice  where invoice_id=?';
            $rest = $G_PGLOB->DBCA->stmt_query($delSql,'i',$invoice_id);
            if($rest==1){
                die(json_encode(array('code' => 200, 'message' => '删除成功')));
            }else{
                die(json_encode(array('code' => 400, 'message' => '删除失败')));
            }

        } catch (Exception $e) {
            $G_PGLOB->DBCA->rollBack();
            die(json_encode(array('code' => 400, 'message' => '删除失败')));
        }





    }




}